/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kodi.controller;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.Query;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import kodi.model.User;
import kodi.gen.util.HibernateUtil;
import kodi.model.UserRights.Task;

/**
 *
 * @author Mikeno
 */
public class LoginManager extends HibernateUtil {

    public User verifyLogin(User login) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        String SQL_QUERY = "from User u where u.name='" + login.getName()
                + "' and u.password='" + login.getPassword() + "' and u.status <> '0'";
        User us = null;
        try {
            Query query = session.createQuery(SQL_QUERY);
            Iterator it = query.iterate();
            if (it.hasNext()) {
                us = ((User) it.next());

                System.out.println("Task Sets : " + us.getTasklist());
                System.out.println("Status : " + us.getStatus().getStatus_name());


                for (Iterator i = us.getTasklist().iterator(); i.hasNext();) {
                    Task t = ((Task) (i.next()));
                    t.setModule_id(2);
                    t.setAction_id(1);
                    t.setTask_name("super");
                    t.setSubmodule_id(2);
                    
                }
                session.getTransaction().commit();
                return us;
            }
            } catch  (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();              // rollback any action done.

        }
        session.close();
        return null;
    }
    /*
    public void retrieveTasks(int user_Id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    String SQL_QUERY = "from UserRight u ,Task t where u.user_id = '"+user_Id+"'";
    try {
    Query query = session.createQuery(SQL_QUERY);
    Iterator it = query.iterate();
    while (it.hasNext()) {
    Task s = ((Task) it.next());
    System.out.println("Task :" + s.getTask_name());
    }
    } catch (HibernateException e) {
    e.printStackTrace();
    session.getTransaction().rollback();              // rollback any action done.

    }
    session.getTransaction().commit();
    session.close();
    //return null;
    }
     */

    public User add(User contact) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(contact);
        session.getTransaction().commit();
        return contact;
    }

    public User delete(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        User contact = (User) session.load(User.class, id);
        if (null != contact) {
            session.delete(contact);
        }
        session.getTransaction().commit();
        return contact;
    }
}
